package docview;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.tree.DefaultMutableTreeNode;
import tableprocessor.CathegoryStaticTree;
import tableprocessor.CathegoryStaticTreeFactory;
import tableprocessor.ColumnPare;
import tableprocessor.PriceDAO;

/**
 *
 * @author gleb
 */
public class DocTableManager implements CathegoryEventListener, ActionListener, MouseListener
{
    private DocTable ctable = new DocTable();
    private List<JCheckBoxMenuItem> list = new ArrayList<JCheckBoxMenuItem>();
    private JPopupMenu menu = new JPopupMenu();
    public DocTableManager(DocTable _tbl)
    {
        ctable = _tbl;
    }
    public void selectCathegoryEvent(DefaultMutableTreeNode node)
    {
        CathegoryStaticTree tree = CathegoryStaticTreeFactory.getInstance();
        if (tree.isHasColumns(node))
        {
            menu = ctable.getColorTable().getPopupMenu();
             menu.removeAll();
             list.removeAll(list);
            Iterator<ColumnPare> iter = tree.getColumnsForCathegory(node);
            if (iter != null)
            {
                while (iter.hasNext())
                {
                    ColumnPare cpare = iter.next();
                    JCheckBoxMenuItem mitem = new JCheckBoxMenuItem(cpare.getDbcol().getName());
                    mitem.addActionListener(this);
                    menu.add(mitem);
                    list.add(mitem);
                }
            }
        }
    }

    public void actionPerformed(ActionEvent e)
    {
        for(int i = 0 ; i < list.size(); i++)
        {
            if (list.get(i).equals((JCheckBoxMenuItem)e.getSource()))
            {
                list.get(i).setEnabled(false);
            }
        }

    }

    public void mouseClicked(MouseEvent e)
    {

    }

    public void mousePressed(MouseEvent e)
    {
        if (e.isPopupTrigger())
        {
          int column = ctable.getColorTable().columnAtPoint(e.getPoint());
          int row = ctable.getColorTable().rowAtPoint(e.getPoint());
          System.out.println("row = "+row+"column = "+column);
          menu.show(e.getComponent(), e.getX(), e.getY());

        }
    }

    public void mouseReleased(MouseEvent e) {
        if (e.isPopupTrigger())
        {
          int column = ctable.getColorTable().columnAtPoint(e.getPoint());
          int row = ctable.getColorTable().rowAtPoint(e.getPoint());
          System.out.println("row = "+row+"column = "+column);
          menu.show(e.getComponent(), e.getX(), e.getY());
        }
    }

    public void mouseEntered(MouseEvent e)
    {
        
    }

    public void mouseExited(MouseEvent e) {

    }
}
